package com.wanczy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanczy.pojo.UserRoleTable;
import com.wanczy.service.UserRoleTableService;
import com.wanczy.service.impl.UserRoleTableServiceImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@WebServlet("/FindAllRoleServlet")
public class FindAllRoleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		UserRoleTableService service = new UserRoleTableServiceImpl();
		List<UserRoleTable> list = service.findAllRole();
		JSONArray jsonArray = new JSONArray();
		Iterator<UserRoleTable> it = list.iterator();
		while(it.hasNext()) {
			UserRoleTable userRole = it.next();
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("roleId", userRole.getRole_id());
			jsonObject.put("roleName", userRole.getRole_name());
			jsonArray.add(jsonObject);
		}
		out.println(jsonArray.toString());
		out.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
